package ynu.edu.online_course.mapper;

import org.apache.ibatis.annotations.*;
import ynu.edu.online_course.entity.User;
import java.util.List;

@Mapper
public interface UserMapper {

    // 根据ID查询用户
    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(@Param("id") Long id);

    //根据电话查询用户
    @Select("SELECT * FROM user WHERE telephone = #{telphone}")
    User findByTelephone(@Param("telphone") String telphone);
    // 插入用户
    @Insert("INSERT INTO user (username, telephone, password, enabled) " +
            "VALUES (#{username}, #{telephone}, #{password}, #{enabled})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);

    // 更新用户信息
    @Update("UPDATE user SET username = #{username}, telephone = #{telephone}, " +
            "password = #{password}, enabled = #{enabled} WHERE id = #{id}")
    int updateUser(User user);

    // 删除用户
    @Delete("DELETE FROM user WHERE id = #{id}")
    int deleteUser(@Param("id") Long id);

    // 查询所有用户
    @Select("SELECT * FROM user")
    List<User> findAll();

    // 修改用户状态
    @Update("UPDATE user SET enabled = #{enable} WHERE id = #{user}.id ")
    void updateUserEnabled(@Param("user") User user, @Param("enable") boolean b);

    // 检查是否存在某号码
    @Select("SELECT COUNT(*) FROM user WHERE telephone = #{telephone}")
    boolean existsByTelephone(@Param("telephone") String telephone);


}
